package com.motong.gongdan.config.interceptor;

import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author:fff
 * @time:2022/6/15
 */
@Component
@WebFilter(urlPatterns = "/*",filterName = "channelFilter")
public class ChannelFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        //如果是POST走自己的继承的HttpServletRequestWrapper类请求，否则走正常的请求
        if(StringUtils.equalsIgnoreCase(HttpMethod.POST.name(), servletRequest.getMethod())
                && !servletRequest.getRequestURI().contains("upload") && !servletRequest.getRequestURI().contains("import")&& !servletRequest.getRequestURI().contains("Import")){
            //一定要在判断中new对象，否则还会出现Stream closed问题
            chain.doFilter(new RequestWrapper(servletRequest),response);
        }else{
            chain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}
